<template>
  <div class="main">
    <eagle-input
      label="附件编号"
      name="PartsCode"
      v-model="info.PartsCode"
      isInputText
    ></eagle-input>
    <eagle-input
      label="附件名称"
      name="PartsName"
      v-model="info.PartsName"
      isInputText
    ></eagle-input>
    <eagle-input name="checkbox" label="需要复检">
      <template #input>
        <eagle-checkbox
          v-model="info.IsNeedCheck"
          shape="square"
          @change="checkChange"
        />
      </template>
    </eagle-input>
    <eagle-input
      v-if="info.IsNeedCheck"
      label="最近检验日期"
      v-model="info.CheckDate"
      name="CheckDate"
      placeholder="点击选择日期"
      @click="handleShow('CheckDate')"
      readonly
    ></eagle-input>
    <eagle-input
      v-if="info.IsNeedCheck"
      label="下次检验日期"
      v-model="info.NextCheckDate"
      name="NextCheckDate"
      placeholder="点击选择日期"
      @click="handleShow('NextCheckDate')"
      readonly
    ></eagle-input>
    <div class="btn_submit">
      <eagle-button block type="primary" @click="_submit"> 保存 </eagle-button>
    </div>
    <eagle-calendar
      :min-date="new Date('2000-01-01')"
      :max-date="new Date('2040-01-01')"
      color="#1989fa"
      v-model="showCalendar"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { formatDate, getDate } from "@/utils/getDate";
export default {
  components: {},
  data() {
    return {
      showCalendar: false,
      menuList: [],
      selectKey: "",
      mobile: "",
      message: "",
      value: "",
      radio: "1",
      checkbox: false,
      checkboxGroup: [],
      info: {
        PartsCode: "",
        PartsName: "",
        IsNeedCheck: false,
        equipmentCode: this.$route.query.equipmentCode,
        ID: 0,
      },
      id: "",
      editCheckShow: false,
      addCheckShow: false,
    };
  },
  created() {
    this.initData();
  },

  methods: {
    initData() {
      let _this = this;
      _this.$axios
      .get("/EquEquipment/InitCheckEntity",{})
      .then((res)=>{
        _this.info.CheckDate = getDate(res.data.CheckDate);
        _this.info.NextCheckDate = getDate(res.data.NextCheckDate);
        _this.show = true;
      })
      .catch((err) => {
        console.log("err", err);
      });
    },
    handleShow(name) {
      
      this.showCalendar = true;
      this.selectKey = name;
    },

    onConfirm(date) {
      const dateTime = formatDate(date);
      this.info[this.selectKey] = getDate(dateTime);
      this.showCalendar = false;
    },

    // 保存
    _submit() {
      let _this = this;
      _this.$axios
      .post("/EquEquipment/AddEquSafeFile",_this.info)
      .then((res)=>{
        if (res.success) {
            _this.$toast.success("新增成功！");
            _this.$router.goRefresh(-1);
          }
      })
      .catch((err) => {
        console.log("err", err);
      });
    },
    checkChange(value) {
      if (this.$route.query.equipmentCode) {
        this.editCheckShow = value;
      } else {
        this.addCheckShow = value;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  padding-top: 10px;
}
.input_text .van-field__control {
  color: #999 !important;
}
</style>
